﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： NoticeRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-10-11
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using Dapper;
using Dapper.Extensions;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	///公告 仓储层
	/// </summary>	
	public class NoticeRepository:BaseRepository<Notice>
	{		
    
        #region 构造

        public NoticeRepository() : base(){}

        public NoticeRepository(string connectionString) : base(connectionString){}

        public NoticeRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        public List<Notice> GetNotices(int carGroupID)
        {
            string sql = "SELECT * FROM [Notice] WHERE IsActive=1 AND (CarGroupID=0 OR CarGroupID=" + carGroupID + ")";
            return Query(sql);
        }

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<NoticeDto> GetPages(int pageIndex, long pageSize, ReqNoticeCondition condition)
        {
            StringBuilder sql = new StringBuilder("SELECT n.*,isnull(b.CarGroupName,'所有') as CarGroupName FROM Notice n WITH(NOLOCK) left Join CarGroup b WITH(NOLOCK) on n.CarGroupId=b.CarGroupId where n.IsActive =1 and b.IsActive =1");
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sql.Append(" AND n.CreateDate>@StartDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sql.Append(" AND n.CreateDate<@EndDate");
            }
            if (condition.CarGroupID > -1)
            {
                sql.Append(" AND n.CarGroupID=@CarGroupID");
            }

            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@CarGroupID", condition.CarGroupID, System.Data.DbType.Int32);
            dparams.Add("@StartDate", condition.StartDate, System.Data.DbType.String);
            dparams.Add("@EndDate", condition.EndDate, System.Data.DbType.String);
            return dbContext.Connection.Page<NoticeDto>(pageIndex, pageSize, sql.ToString(), dparams);
        }
    }
}